package com.qianfeng.index_7005.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.qianfeng.entity.Result;
import com.qianfeng.entity.User;
import com.qianfeng.index_7005.dao.LoginDao;
import com.qianfeng.index_7005.service.LoginService;
import com.qianfeng.index_7005.util.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * author: 田晋宇
 * datetime:2021/11/1 11:13
 * email:853796359@qq.com
 */
@Service
public class LoginServiceImpl implements LoginService {


    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private LoginDao loginDao;



    @Override
    public User login(String username, String password) {








        return loginDao.login(username, password);
    }

    @Override
    public Result checkLogin(Integer userid) {


        Result result = new Result();
        if(userid==null){//没登陆
            result.setStatus(1);
            result.setMsg("未登录");
        }else{
            String key="login:user:"+userid;
            User user =(User) redisTemplate.opsForValue().get(key);
            if(user==null){//没登陆
                result.setStatus(1);
                result.setMsg("未登录");
            }else{//登陆了
                result.setStatus(0);
                result.setMsg("已登录");
                result.setData(user);
                redisTemplate.expire(key,30L, TimeUnit.MINUTES);//续redis key 的生存时间
            }

        }
        return result;
    }

    @Override
    public Result logout(Integer userid) {



        Result result = new Result();
        if(userid==null){//未登陆状态
            result.setStatus(1);
            result.setMsg("当前未登陆");

        }else{
            Result res = checkLogin(userid);

            if(res.getStatus()==0){//已经登陆  执行登出
                String key="login:user:"+((User)res.getData()).getId();
                redisTemplate.delete(key);


                result.setStatus(0);
                result.setMsg("登出成功");
            }else{//未登陆状态
                result.setStatus(1);
                result.setMsg("当前未登陆");
            }
        }


        return result;
    }

    @Override
    public Boolean checkEmail(String email) {

        return loginDao.checkEmail(email)==null;
    }

    @Override
    public Integer registByEmail(String email, String password) {
        return loginDao.registByEmail(email,password);
    }

    @Override
    public void send(String tel) throws Exception {



        String random="";
        for (int i = 0; i < 4; i++) {
            long round = Math.round(Math.random() * 9);
            random+=round;
        }


        com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI4G3ccGECC2m2GtsF1QRM", "HiuoiRtfd9ER3ZOvKrvo6p4jN9NEO0");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(tel)
                .setSignName("千锋太原2001")
                .setTemplateCode("SMS_205129151")
                .setTemplateParam("{\"code\":\""+random+"\"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);

        redisTemplate.opsForValue().set(tel,random,1L,TimeUnit.MINUTES);



    }

    @Override
    public Result checkMsg(String tel, String code,String password) {

        String o =(String) redisTemplate.opsForValue().get(tel);

        Result result = new Result();
        if(o!=null&&o.equals(code)){
            result.setStatus(0);
            result.setMsg("校验成功");

            loginDao.registByTel(tel,password);

        }else{
            result.setStatus(1);
            result.setMsg("校验失败");
        }
        return result;
    }

    @Override
    public Boolean checkTel(String tel) {




        return loginDao.checkTel(tel)==null;
    }
}
